package demo1;

import java.util.Stack;

public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void create() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        head = listNode1;
    }

    public void display() {
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        ListNode pre = new ListNode(data);
        pre.next = head;
        head = pre;
    }

    //尾插法
    public void addLast(int data){
        ListNode last = new ListNode(data);
        if (head == null) {
            head = last;
        }else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = last;
        }
    }

    private void checkIndexAdd(int index) throws MySingleListIndexOutOfException{
        if (index < 0 || index > size()) {
            throw new MySingleListIndexOutOfException("插入下标不合法！");
        }
    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = this.head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndexAdd(index);

        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == size()) {
            addLast(data);
            return;
        }

        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOne(index);

//        ListNode next = cur.next;
//        cur.next = node;
//        node.next = next;
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (this.head == null) {
            System.out.println("此时链表为空");
            return;
        }

        if (this.head.val == key) {
            this.head = this.head.next;
        }

        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (this.head == null) {
            System.out.println("此时链表为空");
            return;
        }

        while (this.head != null && this.head.val == key) {
            this.head = this.head.next;
        }

        ListNode cur = this.head;
        while (cur != null && cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }

//        if (this.head.val == key) {
//            this.head = this.head.next;
//        }
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void clear(){
        this.head = null;
    }

    public void display2(ListNode head) {
        if (head == null) {
            return;
        }
//        if (head.next == null) {
//            System.out.println(head.val);
//            return;
//        }
        display2(head.next);
        System.out.println(head.val);
    }

    public void display3() {
        if (this.head == null) {
            return;
        }

        Stack<ListNode> stack = new Stack<>();
        ListNode cur = this.head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }

        while (!stack.empty()) {
            ListNode node = stack.pop();
            System.out.println(node.val);
        }

    }
}
